<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Goods as Goods1;
use app\admin\validate\GoodsValidate;

class Goods extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $db=new Goods1();
        $list=$db->paginate();
        $this->assign('list',$list);
        $this->assign('total',$list->total());
        return $this->fetch();
;    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function addcate()
    {   
        $cate=new Goods1();
        $list = $cate->field('id,title')->select();

        $this->assign('list',$list);

        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data=request()->param();
        $validate=new GoodsValidate();
        if (!$validate->check($data)) {
            return ['code'=>400,'msg'=>$validate->getError()];
        }

        $db=new Goods1();
        $res=$db->_update($data);

        if ($res) {
            return ['code'=>'200','msg'=>'操作成功'];
        }else {
            return ['code'=>'400','msg'=>'操作失败'];
        }   
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $id=request()->param('id');
        $db=new Goods1();
        $info=$db->where('id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id=request()->param('id');
        $db=new Goods1();
        $res=$db->where('id',$id)->delete();

        if ($res) {
            return ['code'=>200,'msg'=>'删除成功'];
        }else {
            return ['code'=>400,'msg'=>'删除失败'];
        }
    }
}
